import { useState,useEffect } from 'react';
import { getTopics } from '../api/cnode';
const useTopics = () => {
    const [list, setList] = useState([]);
    const [loading, setLoading] = useState(false);
    const [page, setPage] = useState(1)
    useEffect(()=>{
        getTopics({page:page})
          .then(res=>{
            setList([...list,...res.data])
            loading(false)
          })
          .catch(()=>{setLoading(false)})
      },[page])
    onEndReached = () => {
        setPage(page + 1)
      }
      onRefresh = () =>{
        setLoading(true);
        
        if(page=1){
          getTopics({page:page})
            .then(res=>{
              setList([...list,...res.data])
              setLoading(false)
            })
            .catch(()=>{})
            .finally(()=>{setLoading(false)})
        }else{
          setList([])
          setPage(1)
        }
      }
    return {loading,list,onEndReached,onRefresh}
}
export default useTopics